package czy.demo.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Transactional
@RequestMapping("/send")
public class SendController {

    private static final Logger logger = LoggerFactory.getLogger(SendController.class);

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    /* 发送数据到默认主题 */
    @PostMapping
    public String sendToDefault(String message)throws Exception{
        ListenableFuture<SendResult<String,String>> result = this.kafkaTemplate.sendDefault(message);
        return result.get().getRecordMetadata().toString();
    }

}
